package com.tanhua.dubbo.api.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.dubbo.mapper.SettingsMapper;
import com.tanhua.dubbo.pojo.Settings;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService(version = "1.0.0")
public class SettingsApiImpl implements SettingsApi {

    @Autowired
    private SettingsMapper settingsMapper;

    /**
     * 根据用户id查询配置
     *
     * @param userId
     * @return
     */
    @Override
    public Settings querySettings(Long userId) {
        QueryWrapper<Settings> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return this.settingsMapper.selectOne(queryWrapper);
    }

    /**
     * 更新设置
     *
     * @param userId
     * @param likeNotification
     * @param pinglunNotification
     * @param gonggaoNotification
     */
    @Override
    public void updateNotifications(Long userId, Boolean likeNotification, Boolean pinglunNotification, Boolean gonggaoNotification) {
        QueryWrapper<Settings> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);

        Settings settings = this.settingsMapper.selectOne(queryWrapper);
        if (ObjectUtil.isEmpty(settings)){
            //如果设置为空。插入一条新的设置
            settings = new Settings();
            settings.setId(userId);
            this.settingsMapper.insert(settings);
        }else {
            //如果设置不为空，更新设置
            settings.setLikeNotification(likeNotification);
            settings.setPinglunNotification(pinglunNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            this.settingsMapper.update(settings,queryWrapper);
        }

    }
}
